//
//  SRWordTranslateView.m
//  StoryShow
//
//  Created by CyonLeuMBP on 2017/5/1.
//  Copyright © 2017年 qudiandu.cn. All rights reserved.
//

#import "SRWordTranslateView.h"
#import <AVFoundation/AVFoundation.h>

@interface SRWordTranslateView ()

@property (weak, nonatomic) IBOutlet UILabel *wordLabel;
@property (weak, nonatomic) IBOutlet UIButton *voiceButton;
@property (weak, nonatomic) IBOutlet UILabel *phonticLabel;
@property (weak, nonatomic) IBOutlet UITextView *tranlateTextView;

@property (strong, nonatomic)  AVPlayer *audioStreamer;
@property (strong, nonatomic)  SRYoudaoTranslateBasicModel *basicModel;

@end
@implementation SRWordTranslateView

- (void)awakeFromNib {
    [super awakeFromNib];
    
    FZStyleSheet *css = [FZStyleSheet currentStyleSheet];
    
    self.backgroundColor = css.color_1;
    self.wordLabel.textColor = css.color_4;
    self.phonticLabel.textColor = css.color_4;
    self.tranlateTextView.textColor = css.color_4;
    
    self.wordLabel.font = css.fontOfH1;
    self.phonticLabel.font = css.fontOfH4;
    self.tranlateTextView.font = css.fontOfH4;
    self.tranlateTextView.backgroundColor = css.color_1;
    
    [self.voiceButton addTarget:self action:@selector(onVoiceButton:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)setWord:(NSString *)word {
    _word = word;
    
    self.wordLabel.text = word;
}

- (void)resetTranslateView {
    self.wordLabel.text = nil;
    self.phonticLabel.text = nil;
    self.tranlateTextView.text = nil;
    self.word = nil;
}

- (void)onVoiceButton:(id)sender {
    [self playWordVoiceWithWord:self.word];

}

- (void)playWordVoiceWithWord:(NSString *)word {
    if (word && word.length >0) {
        NSString  *youdaoString = [NSString  stringWithFormat:@"http://dict.youdao.com/dictvoice?audio=%@", word];
        NSURL *videoUrl = [NSURL URLWithString:youdaoString];
        AVAsset *asset = [AVURLAsset URLAssetWithURL:videoUrl options:nil];
        AVPlayerItem *anItem = [AVPlayerItem playerItemWithAsset:asset];
        self.audioStreamer = [AVPlayer playerWithPlayerItem:anItem];
        
        AVAudioSession *session = [AVAudioSession sharedInstance];
        NSError *setCategoryError = nil;
        if (![session setCategory:AVAudioSessionCategoryPlayback
                      withOptions:AVAudioSessionCategoryOptionMixWithOthers
                            error:&setCategoryError]) {
        }
        [self.audioStreamer play];
    }

}

- (void)updateTranslateInfo:(SRYoudaoTranslateBasicModel *)model {
    
    if (!model) {
        return;
    }
    
    self.basicModel = model;
    
    self.phonticLabel.text = [NSString stringWithFormat:@"/%@/", self.basicModel.phonetic ];
    
//    self.tranlateTextView.text = self.basicModel.phonetic;
    NSMutableString *resultString = [[NSMutableString  alloc] init];

    for (NSString  *expain  in self.basicModel.explains )
    {
//        NSString* strWord = [NSString stringWithFormat:@"%@\n",expain];
//        NSMutableAttributedString *attriResultStr = [[NSMutableAttributedString alloc] initWithString:strWord];
//        [attributedString appendAttributedString:attriResultStr];
        [resultString appendFormat:@"%@\n", expain];
    }
    
    self.tranlateTextView.text = resultString;
}


@end
